package com.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dao.DepDAO;
import com.pojo.Dep;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DepService  extends ServiceImpl<DepDAO, Dep> {

    @Autowired
    private  DepDAO depDAO;

    /**查缓存--存缓存
     * 先到redis缓存里面找，key="dep"+部门id
     * 缓存没有就到数据库找，数据找到了，就存缓存
     * @param id
     * @return
     */
    @Cacheable(value = "dep",key="#id")
    public Dep findById(int id)
    {
        return depDAO.selectById(id);
    }


    /**
     * 删缓存
     * @param id
     * @return
     */
    @CacheEvict(value = "dep",key="#id",beforeInvocation = true)
    public int delById(int id)
    {
        return depDAO.deleteById(id);
    }


    /**
     * 只存缓存(新增和修改)
     * @param dep
     * @return
     */
    @CachePut(value = "dep",key="#dep.depId")
    public int saveDep(Dep dep)
    {
        return depDAO.insert(dep);
    }


}
